import 'package:flutter/material.dart';

class ColorTap extends StatefulWidget {
  final Color backgroundColor;
  final Color pressedColor;
  final Function onTap;
  final Widget child;

  const ColorTap({
    Key key,
    this.backgroundColor = Colors.white,
    this.pressedColor,
    this.onTap,
    this.child,
  }) : super(key: key);

  @override
  _ColorTapState createState() => _ColorTapState();
}

class _ColorTapState extends State<ColorTap> {
  bool _isDown = false;

  @override
  Widget build(BuildContext context) {
    Color pressedColor = widget.pressedColor ?? Colors.grey.withOpacity(0.3);

    return GestureDetector(
      behavior: HitTestBehavior.translucent,
      child: AnimatedContainer(
        duration: const Duration(milliseconds: 200),
        color: _isDown ? pressedColor : widget.backgroundColor,
        child: widget.child,
      ),
      onTap: widget.onTap,
      onTapDown: (_) => setState(() => this._isDown = true),
      onTapUp: (_) => setState(() => this._isDown = false),
      onTapCancel: () => setState(() => this._isDown = false),
    );
  }
}
